package com.learn.security.api.controller;

import com.learn.security.api.dto.RegisterDTO;
import com.learn.security.app.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

/**
 * @author PC
 * 登录注册Controller
 */
@RestController("loginController.v1")
@RequestMapping("/v1/login")
public class LoginController {
    private LoginService loginService;

    @Autowired
    public void setLoginService(LoginService loginService) {
        this.loginService = loginService;
    }

    @PostMapping("/register")
    public ResponseEntity<RegisterDTO> register(@RequestBody RegisterDTO registerDTO) {
        return ResponseEntity.ok(loginService.register(registerDTO));
    }

    @GetMapping("/user-info")
    public ResponseEntity<Authentication> userInfo(){
        SecurityContext securityContext = SecurityContextHolder.getContext();
        return ResponseEntity.ok(securityContext.getAuthentication());
    }
}
